package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.constant.ParkFeatureConstant;
import com.tbit.main.pojo.ParkFeature;
import com.tbit.main.service.ParkFeatureService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * 站点功能配置
 *
 * @author LMY
 * @create 2021-09-06 10:00
 */
@RestController
@RequestMapping("/ParkFeatureController")
public class ParkFeatureController {
    @Autowired
    private ParkFeatureService parkFeatureService;

    /**
     * 添加
     *
     * @param token
     * @param parkFeature
     * @return
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.PARKFEATURE, methods = MethodEnum.PARKFEATURE_ADD)
    public JsonResult add(String token, ParkFeature parkFeature) {
        Assert.notNull(parkFeature.getParkPointId(), "ParkPoint.id.notNull");

        List<ParkFeature> parkFeatureDB = parkFeatureService.getByParkPointId(parkFeature.getParkPointId());
        if (parkFeatureDB != null && parkFeatureDB.size() > 0) {
            parkFeatureService.del(parkFeature.getParkPointId());
        }

        if (parkFeature.getFeatureTypes() != null && parkFeature.getFeatureTypes().size() > 0) {
            List<ParkFeature> parkFeatures = new LinkedList<>();
            for (Integer a : parkFeature.getFeatureTypes()) {
                ParkFeature park = new ParkFeature();
                park.setParkPointId(parkFeature.getParkPointId());
                park.setFeatureType(a);
                park.setEnable(1);
                if (a.equals(ParkFeatureConstant.BLY) && parkFeature.getPropert() != null) {
                    park.setPropert("rssi=" + parkFeature.getPropert());
                } else {
                    park.setPropert("0");
                }
                park.setUpdateTime(DateTimeUtil.getNowTime());
                parkFeatures.add(park);
            }
            parkFeatureService.addBatch(parkFeatures);
        }

        return JsonResult.succ();
    }

    /**
     * 根据站点id查询
     *
     * @param token
     * @param parkPointId
     * @return
     */
    @RequestMapping("/getByParkPointId")
    public JsonResult getByParkPointId(String token, Integer parkPointId) {

        Assert.notNull(parkPointId, "ParkPoint.id.notNull");

        List<ParkFeature> parkFeatures = parkFeatureService.getByParkPointId(parkPointId);

        ParkFeature parkFeature = new ParkFeature();
        if (parkFeatures != null && parkFeatures.size() > 0) {

            List<Integer> type = new LinkedList<>();
            for (ParkFeature park : parkFeatures) {
                if (park.getEnable().equals(1)) {
                    type.add(park.getFeatureType());
                    if (park.getFeatureType().equals(ParkFeatureConstant.BLY) && park.getPropert() != null) {
                        String str1 = park.getPropert().substring(0, park.getPropert().indexOf("="));
                        String str2 = park.getPropert().substring(str1.length() + 1, park.getPropert().length());
                        parkFeature.setPropert(str2);
                    }
                }
            }
            parkFeature.setParkPointId(parkPointId);
            parkFeature.setFeatureTypes(type);
        }

        return JsonResult.succ(parkFeature);
    }
}
